package demo.DataStrucAndAlgo.Stack;

import java.util.Arrays;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/7/22-07-22-16:53
 * @Description：demo.DataStrucAndAlgo
 */
class MyStack{
    int Stack_Size = 4;
    int stack_length = 0;           //当前栈的大小
    int[] stack = new int[Stack_Size];
    public void pop(){        //pop:弹栈 将栈顶数据弹出
        if(empty()){
            System.out.println("弹栈失败，当前为空栈！");
        }else{
            System.out.println(stack[stack_length-1]);
            stack_length--;
        }
    }
    public void push(int num){      //push:压栈 将值放入栈中
        if (isFull()){
            grow();
            System.out.println("当前栈已满,将栈进行扩容，栈大小为：" + Stack_Size);
        }
        stack[stack_length] = num;
        stack_length++;
    }
    public int getSize(){       //size() 获取栈容量
        return stack_length;
    }
    public void show(){
        if(empty()){
            System.out.println("当前栈为空！");
        }
        for (int i = stack_length-1; i >= 0; i--) {
            System.out.print(stack[i] + "\t");
        }
    }
    public boolean empty(){
        return stack_length == 0;
    }
    public boolean isFull(){
        return stack_length == Stack_Size;
    }
    public void grow(){
        // 100    200
        stack = Arrays.copyOf(stack,stack.length*2);
        Stack_Size = stack.length;
//        stack_length = ; // 10
    }
}
public class Stack_array {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        myStack.pop();
        myStack.show();

        System.out.println("当前栈已容纳： " + myStack.getSize());
    }

}
